package org.zzz.service.impl;


import org.apache.ibatis.session.SqlSession;
import org.zzz.entity.*;
import org.zzz.entity.vo.StudentShowVo;
import org.zzz.mapper.StudentMapper;
import org.zzz.mapper.TeacherMapper;
import org.zzz.mapper.UserMapper;
import org.zzz.service.UserService;
import org.zzz.utils.MybatisUtil;

import java.util.List;

/**
 * @Author ZZF
 * @Time 2021/04/19 15:15
 */
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;
    private StudentMapper studentMapper;
    private TeacherMapper teacherMapper;

    {
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
        userMapper = MybatisUtil.getMapper(UserMapper.class);
        studentMapper = MybatisUtil.getMapper(StudentMapper.class);
        teacherMapper = MybatisUtil.getMapper(TeacherMapper.class);
    }

    @Override
    public List<User> getUser() {
        return userMapper.getUser();
    }

    @Override
    public User login(String username, String password) {
        return userMapper.getUserByUsernameAndPassword(username, password);
    }

    @Override
    public boolean insertUser(User user) {
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    @Override
    public boolean deleteUser(String username, int flag) {
        int insertUser = userMapper.deleteUserByUsername(username);
        if(flag == 1){
            int insertStudent = studentMapper.deleteStudentById(username);
            return (insertUser > 0 && insertStudent > 0) ? true : false;
        }else if(flag == 2){
            int insertTeacher = teacherMapper.deleteTeacherById(username);
            return (insertUser > 0 && insertTeacher > 0) ? true : false;
        }else{
            return false;
        }
    }

    @Override
    public TeacherVo getTeacherById(String username) {
        User user = userMapper.getUserByUsername(username);
        Teacher teacher = teacherMapper.getTeacherById(username);
        return user != null && teacher!= null ? new TeacherVo(username,teacher.getName(),teacher.getGender(),teacher.getDegree()) : null;
    }

    @Override
    public StudentVo getStudentById(String username) {
        User user = userMapper.getUserByUsername(username);
        Student student = studentMapper.getStudentById(username);
        return user!= null && student!=null ? new StudentVo(username,student.getName(),student.getGender(),student.getClassId()) : null;
    }

    @Override
    public boolean updateTeacher(TeacherVo teacherVo) {
        return teacherMapper.updateTeacher(
                new Teacher(teacherVo.getUsername(), teacherVo.getName(),teacherVo.getGender(),teacherVo.getDegree())) > 0;
    }

    @Override
    public boolean updateStudent(StudentVo studentVo) {
        return studentMapper.updateStudent(new Student(studentVo.getUsername(),studentVo.getName(),studentVo.getGender(),studentVo.getClassId())) > 0;
    }

    @Override
    public boolean updateUserById(String sourceId, String targetId) {
        return userMapper.updateUserById(sourceId, targetId) > 0;
    }
}
